const Joi = require('joi');
const { password, objectId } = require('./custom.validator');
const { roles } = require('../config/roles');

/**
 * 创建用户验证
 */
const createUser = {
  body: Joi.object().keys({
    name: Joi.string().required(),
    email: Joi.string().required().email(),
    password: Joi.string().required().custom(password),
    role: Joi.string().valid(...roles).required(),
  }),
};

/**
 * 获取用户列表验证
 */
const getUsers = {
  query: Joi.object().keys({
    name: Joi.string().optional(),
    role: Joi.string().optional(),
    isActive: Joi.string().valid('true', 'false').optional(),
    isDeleted: Joi.string().valid('true', 'false').optional(),
    goal: Joi.string().valid('减脂', '增肌', '健康饮食').optional(),
    search: Joi.string().allow('').optional(),
    sortBy: Joi.string().optional(),
    limit: Joi.number().integer().optional(),
    page: Joi.number().integer().optional(),
  }),
};

/**
 * 获取单个用户验证
 */
const getUser = {
  params: Joi.object().keys({
    userId: Joi.string().custom(objectId),
  }),
};

/**
 * 更新用户验证
 */
const updateUser = {
  params: Joi.object().keys({
    userId: Joi.required().custom(objectId),
  }),
  body: Joi.object()
    .keys({
      name: Joi.string(),
      nickname: Joi.string().max(50),
      email: Joi.string().email(),
      password: Joi.string().custom(password),
      role: Joi.string().valid(...roles),
      gender: Joi.number().valid(0, 1, 2),
      height: Joi.number().min(0).max(300),
      weight: Joi.number().min(0).max(500),
      age: Joi.number().min(0).max(150),
      activityLevel: Joi.number().valid(1, 2, 3, 4, 5),
      goal: Joi.string().valid('减脂', '增肌', '健康饮食'),
      dailyCalorieGoal: Joi.number().min(0),
      isActive: Joi.boolean(),
      avatar: Joi.string().uri({ scheme: [/https?/] }).allow('').optional(),
    })
    .min(1),
};

/**
 * 逻辑删除用户验证
 */
const softDeleteUser = {
  params: Joi.object().keys({
    userId: Joi.string().custom(objectId),
  }),
};

/**
 * 删除用户验证
 */
const deleteUser = {
  params: Joi.object().keys({
    userId: Joi.string().custom(objectId),
  }),
};

/**
 * 更新微信小程序用户健康信息验证
 */
const updateMiniprogramUserProfile = {
  body: Joi.object().keys({
    nickname: Joi.string().max(50),
    avatar: Joi.string().max(500),
    gender: Joi.number().valid(0, 1, 2),
    height: Joi.number().min(0).max(300),
    weight: Joi.number().min(0).max(500),
    age: Joi.number().min(0).max(150),
    activityLevel: Joi.number().valid(1, 2, 3, 4, 5),
    goal: Joi.string().valid('减脂', '增肌', '健康饮食'),
    dailyCalorieGoal: Joi.number().min(0)
  }).min(1),
};

/**
 * 获取微信小程序用户健康信息验证
 */
const getMiniprogramUserProfile = {
  // 无需参数验证，使用token获取用户ID
};

/**
 * 上传用户头像验证
 */
const uploadAvatar = {
  body: Joi.object().keys({
    userId: Joi.string().custom(objectId).optional(),
  }),
  // 头像文件本身的验证（如类型、大小）由 multer 中间件处理
};

module.exports = {
  createUser,
  getUsers,
  getUser,
  updateUser,
  softDeleteUser,
  deleteUser,
  updateMiniprogramUserProfile,
  getMiniprogramUserProfile,
  uploadAvatar,
}; 